import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/Home";
import Welcome from "../views/Welcome";
import Users from "../views/Users";
import Rights from "../views/Rights";
import Roles from "../views/Roles";
import Categories from "../views/Categories";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Categories
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  const tokenStr = window.sessionStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else if (!tokenStr) {
    next('/login')
  } else {
    next()
  }
})

export default router
